package com.igeek.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.entity.Assets;
import com.igeek.boot.entity.Outgoingstock;
import com.igeek.boot.entity.Shelves;
import com.igeek.boot.entity.Warehouses;
import com.igeek.boot.mapper.OutgoingstockMapper;
import com.igeek.boot.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.boot.vo.PageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jwj
 * @since 2023-12-26
 */
@Service
public class OutgoingstockServiceImpl extends ServiceImpl<OutgoingstockMapper, Outgoingstock> implements IOutgoingstockService {

    @Autowired
    private IAssetsService assetsService;

    @Autowired
    private IShelvesService shelvesService;

    @Autowired
    private IWarehousesService warehousesService;

    @Autowired
    private ICustomersService customersService;

    //按条件进行分页查询
    @CachePut(cacheNames = "outgoing",keyGenerator = "keyGenerator")
    @Transactional(readOnly = true)
    @Override
    public Page<Outgoingstock> findPage(PageQueryVO pageQueryVO) {
        //封装分页条件
        Page<Outgoingstock> page = new Page<>(pageQueryVO.getCurrentPage(), pageQueryVO.getPageSize());

        //封装查询条件 根据类型和名称进行模糊查询
        LambdaQueryWrapper<Outgoingstock> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.hasLength(pageQueryVO.getQueryString()), Outgoingstock::getOutgoingName, pageQueryVO.getQueryString())
                .or()
                .like(StringUtils.hasLength(pageQueryVO.getQueryString()), Outgoingstock::getOutgoingType, pageQueryVO.getQueryString());

        //执行查询
        Page<Outgoingstock> pageResult = this.page(page, lqw);

        //添加货架名称  和仓库名称
        List<Outgoingstock> collect = pageResult.getRecords().stream().map(item -> {
            if (ObjectUtils.isNotEmpty(item.getShelfID())) {
                //获取货架信息 -->  填写货架名  --> 设置货架的仓库名
                Shelves shelves = shelvesService.getById(item.getShelfID());
                item.setShelfName(shelves.getShelfName());
                item.setWarehouseName(warehousesService.getById(shelves.getWarehouseId()).getWarehouseName());
                //获取客户名称
                String customerName = customersService.getById(item.getCustomerID()).getCustomerName();
                item.setCustomerName(customerName);
            }
            return item;
        }).collect(Collectors.toList());
        pageResult.setRecords(collect);
        return pageResult;
    }

    //添加出库订单信息
    @Transactional
    @Override
    public boolean addOutgoingStockInfo(Outgoingstock outgoingstock, Integer assetID) {
        //1.随机生成订单号
        String orderNumber = "O" + UUID.randomUUID();
        outgoingstock.setOutgoingNumber(orderNumber);
        //2.根据物资id查询物资的信息，并将物资的名称类型 以及货架id赋值给出库单
        if (ObjectUtils.isNotEmpty(assetID)) {
            Assets assets = assetsService.getById(assetID);
            outgoingstock.setOutgoingName(assets.getAssetName());
            outgoingstock.setOutgoingType(assets.getAssetType());
            outgoingstock.setShelfID(assets.getShelfId());
            return baseMapper.insert(outgoingstock) > 0;
        }
        return false;
    }


    //确定出库操作  --> 出库
    @Transactional
    @Override
    public boolean confirmOutgoingStock(Outgoingstock outgoingstock) {
        //1.根据物资名称类型和货架查询物资的详细信息
        Assets assets = assetsService.selectByShelfIdAndNameType(outgoingstock.getShelfID(), outgoingstock.getOutgoingName(), outgoingstock.getOutgoingType());
        //2.判断出库数量是否大于库存数量  小于等于库存  可以出库
        if (assets.getQuantity() >= outgoingstock.getOutgoingQuantity()) {
            if (baseMapper.updateById(outgoingstock) > 0) {
                //3.更新物资的数量
                assets.setQuantity(assets.getQuantity() - outgoingstock.getOutgoingQuantity());
                assetsService.updateById(assets);
                //更新出库订单状态
                return baseMapper.updateById(outgoingstock)>0;
            }

        }
        //出库失败
        return false;
    }

    //确认修改出库订单信息
    @Transactional
    @Override
    public boolean confirmUpdateOutgoingStock(Outgoingstock outgoingstock, Integer assetID) {
        //1.先进行查询物资的信息
        Assets assets = assetsService.findOneById(assetID);
        if (ObjectUtils.isNotEmpty(assets)) {
            //2.将货架ID存入出库单
            outgoingstock.setShelfID(assets.getShelfId());
            outgoingstock.setOutgoingName(assets.getAssetName());
            outgoingstock.setOutgoingType(assets.getAssetType());
            return baseMapper.updateById(outgoingstock) > 0;
        }
        return false;
    }
}
